//
//  BoZeAddCarVC.swift
//  BoZeCalendar
//
//  Created by 李东 on 2024/4/11.
//

import UIKit
import SnapKit

class BoZeAddCarVC: BoZeBaseVC {
    
    var collectionView: UICollectionView!
    let CellID = NSStringFromClass(BoZeAddCarCollectionCell.self)
    var carNameTF: UITextField!
    var carDisplacementTF: UITextField!
    lazy var summitBtn = UIButton(type: .custom)
    
    var selectIndex: Int = 0
    
    lazy var typeList = [
        BoZeTypeModel(typeName: "汽车", isSelect: true, selectIconName: "car_qc_select", unSelectIconName: "car_qc_unselect", normalIconName: "car_qc_normal"),
        BoZeTypeModel(typeName: "摩托车", isSelect: false, selectIconName: "car_mtc_select", unSelectIconName: "car_mtc_unselect", normalIconName: "car_mtc_normal"),
        BoZeTypeModel(typeName: "货车", isSelect: false, selectIconName: "car_hc_select", unSelectIconName: "car_hc_unselect", normalIconName: "car_hc_normal"),
        BoZeTypeModel(typeName: "三轮车", isSelect: false, selectIconName: "car_slc_select", unSelectIconName: "car_slc_unselect", normalIconName: "car_slc_normal"),
        BoZeTypeModel(typeName: "挖掘机", isSelect: false, selectIconName: "car_wjj_select", unSelectIconName: "car_wjj_unselect", normalIconName: "car_wjj_normal")
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        hideNavgaitionBar()
        showBgImgView = true
        bgImgView.image = UIImage(named: "img_me_bg")
        showPopBtn = true
        navTitle = "加油啦-油耗管家"
        configUI()
    }


    
    @objc func summitHandle() {
        if carNameTF.text?.isEmpty ?? true {
            showHUDTextOnly("请输入车辆名称")
            return
        }
        if carDisplacementTF.text?.isEmpty ?? true {
            showHUDTextOnly("请输入排量")
            return
        }
        if BoZeDataHelper.shared.getLocalList().contains(where: {$0.carName == carNameTF.text!}) {
            showHUDTextOnly("当前名称已添加，请重新输入")
            return
        }
        let seletM = typeList[selectIndex]
        let model = BoZeCarModel(ids:Int(Date().timeIntervalSince1970) ,carIcon: seletM.normalIconName, carType: seletM.typeName, carName: carNameTF.text!)
        BoZeDataHelper.shared.addData(model: model)
        showHUDTextOnly("已保存") {[weak self] in
            self?.navigationController?.popViewController(animated: true)
        }
    }
    
    func configUI() {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize(width: (CGFloat.screenWidth-74)/5, height: 80)
        layout.scrollDirection = .horizontal
        
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: "BoZeAddCarCollectionCell", bundle: nil), forCellWithReuseIdentifier: CellID)
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.left.equalTo(17)
            make.right.equalTo(-17)
            make.top.equalTo(CGFloat.navHeight + 20)
            make.height.equalTo(80)
        }
        
        let nameView = createItemView(title: "车辆名称：", placeholder: "请输入车辆名称")
        view.addSubview(nameView)
        nameView.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(collectionView.snp.bottom).offset(50)
            make.height.equalTo(58)
        }
        
        let displacementView = createItemView(title: "排量：", placeholder: "例如：2.0T")
        view.addSubview(displacementView)
        displacementView.snp.makeConstraints { make in
            make.left.right.height.equalTo(nameView)
            make.top.equalTo(nameView.snp.bottom).offset(15)
        }
        
        summitBtn.backgroundColor = .init(hex: 0x89BB85)
        summitBtn.layer.cornerRadius = 28
        summitBtn.layer.masksToBounds = true
        summitBtn.titleLabel?.font = UIFont.Medium(ofSize: 16)
        summitBtn.setTitleColor(.black, for: .normal)
        summitBtn.setTitle("提交保存", for: .normal)
        summitBtn.addTarget(self, action: #selector(summitHandle), for: .touchUpInside)
        view.addSubview(summitBtn)
        summitBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(displacementView.snp.bottom).offset(60)
            make.width.equalTo(230)
            make.height.equalTo(56)
        }
        
    }
    
    func createItemView(title: String, placeholder: String) -> UIView {
        let nameView = UIView()
        nameView.backgroundColor = .white
        nameView.layer.cornerRadius = 15
        nameView.layer.masksToBounds = true
 
        let label = UILabel()
        label.font = UIFont.Medium(ofSize: 16)
        label.textColor = .black
        label.text = title
        nameView.addSubview(label)
        label.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.width.equalTo(80)
            make.centerY.equalToSuperview()
        }
        
        let tf = UITextField()
        tf.placeholder = placeholder
        tf.textColor = .black
        tf.font = UIFont.systemFont(ofSize: 16)
        tf.textAlignment = .right
        tf.borderStyle = .none
        nameView.addSubview(tf)
        tf.snp.makeConstraints { make in
            make.left.equalTo(label.snp.right).offset(10)
            make.right.equalTo(-15)
            make.top.bottom.equalToSuperview()
        }
        
        if title.contains("排量") {
            carDisplacementTF = tf
        }else{
            carNameTF = tf
        }
        return nameView
    }

}

extension BoZeAddCarVC: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        typeList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellID, for: indexPath) as! BoZeAddCarCollectionCell
        cell.model = typeList[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectIndex = indexPath.row
        let selectModel = typeList[selectIndex]
        typeList.forEach { mm in
            mm.isSelect = mm.typeName == selectModel.typeName
        }
        collectionView.reloadData()
    }
}
